package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.YogaAdminBO;
import com.yoga.dao.dao.YogaAdminDao;
import com.yoga.dao.domain.YogaAdminDO;
import com.yoga.manager.admin.AdminYogaAdminManager;
import com.yoga.manager.beanUtil.YogaAdminBeanUtil;
import com.yoga.manager.uitl.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * Created by hdx on 2020/9/28.
 */
@Component
public class AdminYogaAdminManagerImpl implements AdminYogaAdminManager {
    Logger logger = LoggerFactory.getLogger(AdminYogaAdminManagerImpl.class);

    @Resource
    private YogaAdminDao yogaAdminDao;

    @Override
    public YogaAdminBO adminRegister(YogaAdminBO yogaAdminBO) throws AppException {
        if (yogaAdminBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(yogaAdminBO.getAdminNo())) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_NULL);
        }
        if (StringUtils.isEmpty(yogaAdminBO.getAdminPasswd())) {
            throw new AppException(AppErrorCodeEnum.PASSWD_NULL);
        }
        if (StringUtils.isEmpty(yogaAdminBO.getAdminNick())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        YogaAdminDO old = yogaAdminDao.findByAdminNo(yogaAdminBO.getAdminNo());
        if (old != null) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_IS_EXIST);
        }
        YogaAdminDO yogaAdminDO = YogaAdminBeanUtil.yogaAdminBOToDO(yogaAdminBO);
        yogaAdminDO.setAdminPasswd(MD5Util.passWord(yogaAdminBO.getAdminPasswd()));
        int num = yogaAdminDao.insert(yogaAdminDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }

        return YogaAdminBeanUtil.yogaAdminDOToBO(yogaAdminDao.findById(yogaAdminDO.getId()));
    }

    @Override
    public YogaAdminBO login(YogaAdminBO yogaAdminBO) throws AppException {
        if (yogaAdminBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(yogaAdminBO.getAdminNo())) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_NULL);
        }
        if (StringUtils.isEmpty(yogaAdminBO.getAdminPasswd())) {
            throw new AppException(AppErrorCodeEnum.PASSWD_NULL);
        }
        YogaAdminDO old = yogaAdminDao.findByAdminNo(yogaAdminBO.getAdminNo());
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_NOT_EXIST);
        }
        if (!old.getAdminPasswd().equals(MD5Util.passWord(yogaAdminBO.getAdminPasswd()))) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_OR_PASSWD_ERROR);
        }

        return YogaAdminBeanUtil.yogaAdminDOToBO(old);
    }

    @Override
    public YogaAdminBO getById(Long yogaAdminId) throws AppException {
        if (yogaAdminId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }

        return YogaAdminBeanUtil.yogaAdminDOToBO(yogaAdminDao.findById(yogaAdminId));
    }

    @Override
    public Boolean updateById(YogaAdminBO yogaAdminBO) throws AppException {
        if (yogaAdminBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (yogaAdminBO.getYogaAdminId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        YogaAdminDO old = yogaAdminDao.findById(yogaAdminBO.getYogaAdminId());
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        YogaAdminDO update = YogaAdminBeanUtil.yogaAdminBOToDO(yogaAdminBO);

        if (!StringUtils.isEmpty(yogaAdminBO.getOldPasswd())) {
            if (!old.getAdminPasswd().equals(MD5Util.passWord(yogaAdminBO.getOldPasswd()))) {
                throw new AppException(AppErrorCodeEnum.OLD_PASSWD_ERROR);
            }
            update.setAdminPasswd(MD5Util.passWord(yogaAdminBO.getAdminPasswd()));
        }
        int num = yogaAdminDao.updateById(update);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }
}
